package zhieasy.cn.xpu_helper.controller.common;

import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.WxMaUserService;
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import cn.binarywang.wx.miniapp.bean.WxMaUserInfo;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zhieasy.cn.service.common.WechatUserService;

/**
 * 微信授权登录
 * @author zouchanglin
 */
@Api(tags = "common模块，微信登录等功能操作接口")
@Slf4j
@RestController
@RequestMapping("/common/wechat")
public class WeChatController {

    @Autowired
    WechatUserService wechatUserService;

    private final WxMaService wxMaService;

    public WeChatController(WxMaService wxMaService) {
        this.wxMaService = wxMaService;
    }

    /**
     * @param code 前端传入
     * https://developers.weixin.qq.com/miniprogram/dev/api/open-api/login/wx.login.html
     * @return returnUrl;
     */

    @ApiOperation(
        value = "/common/wechat/login",
        notes = "所有请求前先进行登录，或者等返回token失效错误时请登录",httpMethod = "POST")
    @ApiImplicitParam(paramType="query", name = "wechatCode",
        value = "通过wx.login方法从小程序端获取的code",
        required = true, dataType = "String")
    @ApiResponses({
        @ApiResponse(code=700,message="没有访问权限,请联系管理员"),
        @ApiResponse(code=404,message="请求路径没有或页面跳转路径不对"),
        @ApiResponse(code=200,message="新用户第一次登录成功，但是请紧接着调用第二个方法进行该用户详情的录入，" +
            "返回值中返回了token，请将token存入下一次请求中的header，ResultResponse=>{code:200,message:新用户登录," +
            "object:{token:token对应的字段,sessionKey:sessionKey对应的字段}}"),
        @ApiResponse(code=200,message="老用户登陆成功，返回值中带有token以及session，请将token加入到后面所有的的请求的请求头中" +
            "ResultResponse=>{code:200,message:新用户登录,object:{token:token对应的字段,sessionKey:sessionKey对应的字段}}")
    })
    //@RequestMapping("/login")
    public String login(String code){
        WxMaUserService userService = wxMaService.getUserService();
        try {
            WxMaJscode2SessionResult sessionInfo = userService.getSessionInfo(code);
            String unionId = sessionInfo.getUnionid();
            String openId = sessionInfo.getOpenid();
            String sessionKey = sessionInfo.getSessionKey();
            log.info("【WeChatController】unionId={}, openId={}, sessionKey={}", unionId, openId, sessionKey);
            //TODO do something.
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
//        WechatUser user = wechatUserService.getWechatUserByUnionID(code);
        return "";
    }


    /**
     * 获取用户敏感信息
     * @param sessionKey 会话密钥
     * @param encryptedData 消息密文
     * @param iv 加密算法的初始向量
     * @return returnUrl;
     */
    @ApiOperation(value = "获取用户敏感信息", notes = "微信用户登陆授权后获取用户敏感信息", httpMethod = "POST")
    @RequestMapping("/user-info")
    public String getUserInfo(String sessionKey, String encryptedData, String iv){
        WxMaUserService userService = wxMaService.getUserService();
        WxMaUserInfo userInfo = userService.getUserInfo(sessionKey, encryptedData, iv);
        String unionId = userInfo.getUnionId();
        //TODO do something.
        return "";
    }
}
